/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import bean.PDF;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fernando
 */
public class PDFDao implements Serializable {

    private Conexao conexao;
    private PreparedStatement ps;

    public PDFDao() {
        conexao = new ConexaoPostgres();
    }

    public int cadastrarPDF(PDF pdf) {
        int id = -1;
        try {
            if (this.validarInsercaoPdf(pdf.getNome())) {


                ps = conexao.getConnection().prepareStatement("INSERT INTO pdfs (conteudo, nome) VALUES (?, ?)");
                ps.setBytes(1, pdf.getConteudo());
                ps.setString(2, pdf.getNome());
                ps.executeUpdate();
                conexao.closeConnection();


                ps = conexao.getConnection().prepareStatement("SELECT id FROM pdfs WHERE nome = ?");
                ps.setString(1, pdf.getNome());
                ResultSet rs = ps.executeQuery();
                conexao.closeConnection();

                if (rs.next()) {
                    return rs.getInt("id");

                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(ImagemDao.class.getName()).log(Level.SEVERE, null, ex);
            conexao.closeConnection();
            return id;
        }
        return id;
    }

    public PDF consultarPDF(int id) {
        PDF pdf = null;
        try {
            ps = conexao.getConnection().prepareStatement("SELECT conteudo, nome FROM pdfs WHERE id = ?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            conexao.closeConnection();

            if (rs.next()) {
                pdf = new PDF();
                pdf.setConteudo(rs.getBytes("conteudo"));
                pdf.setNome(rs.getString("nome"));
                pdf.setId(id);


            }

        } catch (SQLException ex) {
            Logger.getLogger(ImagemDao.class.getName()).log(Level.SEVERE, null, ex);
            conexao.closeConnection();
            return pdf;
        }
        return pdf;
    }

    public boolean validarInsercaoPdf(String nomeArquivo) {
        boolean resultado = false;
        try {
            ps = conexao.getConnection().prepareStatement("SELECT id FROM pdfs WHERE nome = ?");
            ps.setString(1, nomeArquivo);
            ResultSet rs = ps.executeQuery();
            conexao.closeConnection();
            if (!rs.next()) {
                resultado = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(PDFDao.class.getName()).log(Level.SEVERE, null, ex);
        }


        return resultado;

    }
    
       
    public boolean removerPDF(int id){
        
        boolean retorno = false;
        
        try {
            ps = conexao.getConnection().prepareStatement("DELETE FROM pdfs WHERE id = ?");
            ps.setInt(1, id);
            ps.executeUpdate();
            conexao.closeConnection();
            retorno = true;
            
        } catch (SQLException ex) {
            Logger.getLogger(PDFDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        return retorno;
        
    }
}
